/**
 * @author SilverBiology, LLC
 * @website http://code.google.com/p/gbif-spreadsheet-processor/
*/
Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial');

GBIF.GeoSpatial.ConfigForm = function(config) {

	Ext.apply(this, config, { 
			labelWidth: 250
		,	border: false
		,	layout: 'fit'
		,	title: "Config Form"
		,	urlSave: 'resources/api/admin.php'
		,	loaded: false
		,	items: [{
					xtype: 'form'
				,	ref: 'cForm'
				,	border: false
				,	layout: 'fit'
				,	reader: new Ext.data.JsonReader({
							root: 'data'
						,	successProperty: 'success'
					}, [
							{name: 'config[title]', type: 'string', mapping: 'title'}
						, {name: 'config[lang][lang]', type: 'string', mapping: 'lang.lang'}
						, {name: 'config[lang][charset]', type: 'string', mapping: 'lang.charset'}
						, {name: 'config[map][name]', type: 'string', mapping: 'map.name'}
						, {name: 'config[map][url]', type: 'string', mapping: 'map.url'}
						, {name: 'config[map][title]', type: 'string', mapping: 'map.title'}
						, {name: 'config[map][version]', type: 'string', mapping: 'map.version'}
						, {name: 'config[mapControl][click]', type: 'string', mapping: 'mapControl.click'}
						, {name: 'config[mapControl][point]', type: 'string', mapping: 'mapControl.point'}
						, {name: 'config[mapControl][line]', type: 'string', mapping: 'mapControl.line'}
						, {name: 'config[mapControl][polygon]', type: 'string', mapping: 'mapControl.polygon'}
						, {name: 'config[mapControl][select]', type: 'string', mapping: 'mapControl.select'}
						, {name: 'config[mapControl][remove]', type: 'string', mapping: 'mapControl.remove'}
						, {name: 'config[mapControl][modify]', type: 'string', mapping: 'mapControl.modify'}
					])
			,	items: {
					xtype:'tabpanel'
				,	activeTab: 0
				,	deferredRender: false
				,	border: false
				,	defaults:{ border: false, bodyStyle:'padding:10px',	autoScroll:true,height:380} 
				,	items:[{
							title:this.txtGeneral
						,	layout:'form'
						,	defaults: {width: 175}
						,	defaultType: 'textfield'
						,	items: [{
									fieldLabel: this.txtTitle
								,	anchor: '95%'
								,	name: 'config[title]'
							},{
									xtype:'combo'
								,	fieldLabel: this.txtLanguage
								,	anchor: '95%'
								,	displayField:'id'
								,	typeAhead:false
								,	mode: 'local'
								,	triggerAction: 'all'
								,	editable:false
								,	hiddenName: 'config[lang][lang]'
								,	valueField:'value'
								,	store: new Ext.data.SimpleStore({
											fields: ['value', 'id']
										,	data: [
													['en', 'English']
												,	['nl', 'Dutch']
												,	['pl', 'Polish']
											]
									})
							},{
									xtype:'combo'
								,	fieldLabel: this.txtCharset
								,	anchor: '95%'
								,	hiddenName:'config[lang][charset]'
								,	displayField:'id'
								,	typeAhead:false
								,	mode: 'local'
								,	triggerAction: 'all'
								,	editable:false
								,	valueField:'value'
								,	store: new Ext.data.SimpleStore({
											fields: ['value', 'id']
										,	data: [['utf-8', 'utf-8']]
									})
							}]
						}, {
								title:"Map Settings"
							,	layout:'form'
							,	defaults: {width: 175}
							,	defaultType: 'textfield'
							,	items: [{
										 	xtype: 'fieldset'
										, 	title: "WMS Base Layer"
										, 	anchor: '95%'
										, 	defaults: {width: 175 }
										, 	defaultType:'textfield'
										, 	items: [{
			  										fieldLabel:"Name"
													,	anchor: '95%'
													,	name: 'config[map][name]'
												},{
														fieldLabel:"Url"
													,	anchor: '95%'
													,	name: 'config[map][url]'
												},{
														fieldLabel:"Title"
													,	anchor: '95%'
													,	name: 'config[map][title]'
												},{
														fieldLabel:"Version"
													,	anchor: '95%'
													,	name: 'config[map][version]'
												}]
										},{
												xtype:'xcheckbox'
											,	fieldLabel: "Show DataGrid Tabs"
											, anchor: '95%'
											, outputValue:false
											,	checked:true
											, name: 'config[report_software_errors]'
										},{
												xtype: 'fieldset'
											, title: "Drawing Controls"
											, layout:'column'
											, anchor: '95%'
											, defaults: {width:225 }
											, defaultType:'xcheckbox'
											, items: [
														{boxLabel: "Click Control", name: 'config[mapControl][click]',checked:true}
												 ,	{boxLabel: "point Control", name: 'config[mapControl][point]',checked:true}
												 , 	{boxLabel: "Line Control", name: 'config[mapControl][line]',checked:true}
												 ,	{boxLabel: "Polygon Control", name: 'config[mapControl][plygon]',checked:true}
												 , 	{boxLabel: "Select Control", name: 'config[mapControl][select]',checked:true}
												 , 	{boxLabel: "Delete Control", name: 'config[mapControl][remove]',checked:true}
												 , 	{boxLabel: "Modify Control", name: 'config[mapControl][modify]',checked:true}
											 ]
									}]
							}]
					}
			}]
		,	bbar: [{
					text: 'Save'
				,	tooltip: 'Click to overwrite config file'
				,	width: 80
				,	iconCls: 'icon_script_save'
				,	listeners: {
						click: {
								scope: this
							,	fn: function(){
									this.submit()
								}
						}
					}
			}]
		,	listeners: {
				activate: function() {
					if(!this.loaded){
						this.cForm.getForm().load({
								url: 'resources/api/load-config.php'
							, waitMsg:'Loading'
							,	params:{
									cmd: 'loadConfig'
								}
						});
						this.loaded=true;	
					}
				}
			}
	});

	GBIF.GeoSpatial.ConfigForm.superclass.constructor.call(this, config);
};

Ext.extend(GBIF.GeoSpatial.ConfigForm, Ext.Panel, {	

		submit: function() {
			var params;
			if(Ext.isEmpty(this.client_id)){
					params = {
						cmd:'saveConfig'
					}	
			}else{
					params = {
							cmd:'saveConfig'
						,	client_id:this.client_id	
					}
			}
			this.cForm.getForm().submit({
					url: 'resources/api/load-config.php'
				,	scope: this
				,	success: this.onSuccess
				,	failure: this.onFailure
				,	params: params
				,	waitMsg: 'Saving...'
			});
			this.loaded=false;
	 }
	 
	,	onSucess:function(){
			console.log("sucess");
		}
		
	,	onFailure:function(){
			console.log("Fail");
		}			 
});